﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Application.Base;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Entities;
using Dora.Services.AdminUser.IApplication;
using Dora.Services.AdminUser.Repository.Repositorys;
using Dora.Tools.Enums.AdminUser;
using Dora.Tools.Utility.Const;
using Microsoft.Extensions.Logging;
using Surging.Core.Common;
using Surging.Core.CPlatform.Ioc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Application
{
    /// <summary>
    /// 大社设置表服务
    /// </summary>
    [ModuleName(DoraModuleName.AdminUser)]
    public class OrgBigSettingService : DoraServiceBase, IOrgBigSettingService
    {
        
        private ISugarUnitOfWork _unitOfWork;
        private readonly OrgBigSettingRepository _repository;
        private readonly OrgGiftSettingRepository _GiftRepository;
        public OrgBigSettingService(ISugarUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _repository = new OrgBigSettingRepository(unitOfWork);
            _GiftRepository = new OrgGiftSettingRepository(unitOfWork);
        }

        /// <summary>
        /// 根据Id获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<OrgBigSettingEntity> GetById(int id)
        {

            var result= await _repository.FirstOrDefaultAsync(id);
            if (result == null)
            {
                result = await Init(id);
            }
            return result;
        }
        /// <summary>
        /// 根据Id和产品类型Id获取配置列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<OrgGiftSettingEntity>> GetGitfById(int id)
        {
            var res= await _unitOfWork.QueryClient.Queryable<OrgGiftSettingEntity>().Where(x => x.Id == id).ToListAsync();
            return res;
        }
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<OrgBigSettingEntity>> GetPageList(OrgBigSettingQueryDto input)
        {
            var page = await _repository.GetPageList(input);
            return page;
        }

        /// <summary>
        /// 根据IdList获取列表
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<List<OrgBigSettingEntity>> GetList(List<int> idList)
        {
            if (idList == null || idList.Count == 0)
            {
                return new List<OrgBigSettingEntity>();
            }
            var list = await _repository.GetAllListAsync(it => idList.Contains(it.Id));
            return list;
        }
        
        /// <summary>
        /// 初始化大社设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<OrgBigSettingEntity> Init(int orgId)
        {
            var oldEntity = await _repository.FirstOrDefaultAsync(it => it.Id == orgId);
            if (oldEntity != null)
            {
                return oldEntity;
            }
            var entity = new OrgBigSettingEntity();
            entity.Id = orgId;
            entity.OrderDefaultOrgId = 0;
            entity.MarketProdMergeType = 0;
            entity.CruiseProdMergeType = 0;
            entity.IsEditProd = 1;
            entity.WxMPTheme = "0";
            entity.IsGiveInsurance = 0;
            entity.UpdatedTime = DateTime.Now;
            var result = await _repository.InsertAsync(entity);
            return result;
        }
        /// <summary>
        /// 初始化 产品类型赠品设置
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public async Task<OrgGiftSettingEntity> InitGift(int orgId,int productTypeId)
        {
            //初始化大社的产品类型赠品设置
            var GiftEntity = await _unitOfWork.QueryClient.Queryable<OrgGiftSettingEntity>().Where(x => x.Id == orgId && x.ProductTypeId == productTypeId).FirstAsync();
            if (GiftEntity != null) return GiftEntity;
            GiftEntity = new OrgGiftSettingEntity
            {
                Id = orgId,
                ProductTypeId = productTypeId,
                IsClose=false
            };
            await _GiftRepository.InsertAsync(GiftEntity);
            return GiftEntity;
        }
        /// <summary>
        /// 修改订单归属门店
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto> UpdateOrderSetting(OrgBigSettingEntity entity)
        {
            if (entity == null)
            {
                return ResultDto.Error("参数传递错误");
            }
            await Init(entity.Id);
            entity.UpdatedTime = DateTime.Now;
            var result = await _repository.UpdateOrderSetting(entity);
            return result? ResultDto.Success(result): ResultDto.Error("更新失败");
        }

        /// <summary>
        /// 修改产品设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto> UpdateProdSetting(OrgSettingDto dto)
        {
            if (dto.OrgBigSettingEntity == null)
            {
                return ResultDto.Error("参数传递错误");
            }
            await Init(dto.OrgBigSettingEntity.Id);
            foreach (var item in dto.OrgGiftSettingList)
            {
                await InitGift(dto.OrgBigSettingEntity.Id,item.ProductTypeId);
            }
           

            dto.OrgBigSettingEntity.UpdatedTime = DateTime.Now;
            var result = await _repository.UpdateProdSetting(dto);
            return result ? ResultDto.Success(result) : ResultDto.Error("更新失败");
        }

        /// <summary>
        /// 微信AppId及秘钥设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto> UpdateWxAppSetting(OrgBigSettingEntity entity)
        {
            if (entity == null)
            {
                return ResultDto.Error("参数传递错误");
            }
            await Init(entity.Id);
            entity.UpdatedTime = DateTime.Now;
            var result = await _repository.UpdateWxAppSetting(entity);
            return result ? ResultDto.Success(result) : ResultDto.Error("更新失败");
        }

        /// <summary>
        /// 更新电子合同是否需要审核
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ResultDto> UpdateEConSetting(OrgBigSettingEntity entity)
        {
            if (entity == null)
            {
                return ResultDto.Error("参数传递错误");
            }
            await Init(entity.Id);
            entity.UpdatedTime = DateTime.Now;
            var result = await _repository.UpdateEConSetting(entity);
            return result ? ResultDto.Success(result) : ResultDto.Error("更新失败");
        }

        
        public async Task<ResultDto<OrgGiftSettingEntity>> GetSettingByTypeId(int orgid, int id)
        {
            if (orgid == 0 || id == 0)
            {
                return ResultDto<OrgGiftSettingEntity>.Error("参数传递错误");
            }
            var res= await _unitOfWork.QueryClient.Queryable<OrgGiftSettingEntity>().Where(x => x.Id == orgid && x.ProductTypeId == id).FirstAsync();
            return ResultDto<OrgGiftSettingEntity>.Success(res);
        }
    }
}
